<?php
namespace app\admin\controller;

use \frame\libs\BaseController;
use app\admin\model\RoleModel;
use app\admin\model\NodeModel;

/**
 * 登录
 */
class RoleController extends BaseController
{ 
    /**
     * 角色管理页
     * @return [type] [description]
     */
    public function index()
    {
        $this->checkPage();
        include VIEW_PATH . 'role/index.html';
    }

    /**
     * 获取所有角色
     * @return [type] [description]
     */
    public function getRoles()
    {
        echo json_encode(RoleModel::getInstance()->getRoles()); 
        return true;
    }

    /**
     * 下拉框获取所有角色
     * @return [type] [description]
     */
    public function getRoleSelect()
    {
        echo $this->success(RoleModel::getInstance()->getRoles(), "success", 200);
        return true;
    }

    /**
     * 根据id获取用户
     * @return [type] [description]
     */
    public function getRoleById()
    {
        echo $this->success(RoleModel::getInstance()->getRoleById($_POST['id']), "success", 200);
        return true;
    }

    /**
     * 获取角色数
     * @return [type] [description]
     */
    public function getRoleNum()
    {
        echo json_encode(RoleModel::getInstance()->getRoleNum());
        return true;
    }

    /**
     * 新增角色
     * @return [type] [description]
     */
    public function insert()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['create_time'] = time();
        $data['update_time'] = time();
        if (RoleModel::getInstance()->insertRole($data)) {
            echo $this->success(null, "添加成功！");
            return true;
        }else{
            echo $this->error(null, "添加失败！");
            return false;
        }
    }

    /**
     * 更新启用状态
     * @return bool [description]
     */
    public function updateStatus()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['update_time'] = time();
        if (RoleModel::getInstance()->updateStatus($data)) {
            echo $this->success(null, "设置成功！");
            return true;
        }else{
            echo $this->error(null, "设置失败！");
            return false;
        }
    }

    /**
     * 删除角色
     * @return [type] [description]
     */
    public function delete()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        if (RoleModel::getInstance()->deleteRoleById($_POST['id'])) {
            echo $this->success(null, "删除成功！");
            return true;
        }else{
            echo $this->error(null, "删除失败！");
            return false;
        }
    }

    /**
     * 更新角色
     * @return bool [description]
     */
    public function update()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['update_time'] = time();
        if (RoleModel::getInstance()->updateRole($data)) {
            echo $this->success(null, "编辑成功！");
            return true;
        }else{
            echo $this->error(null, "编辑失败！");
            return false;
        }
    }

    /**
     * 更新节点
     * @return bool [description]
     */
    public function updateNodes()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data['id'] = $_POST['id'];
        $data['nodes'] = rtrim($_POST['nodes'], ",");
        $data['nodes'] = ltrim($data['nodes'], "null");
        $data['nodes'] = ltrim($data['nodes'], ",");
        $data['update_time'] = time();
        if (RoleModel::getInstance()->updateNodes($data)) {
            echo $this->success(null, "设置成功！");
            return true;
        }else{
            echo $this->error(null, "设置失败！");
            return false;
        }
    }

    /**
     * 获取角色分组, 授权页面
     * @return [type] [description]
     */
    public function getRoleWithGroupsById()
    {
        if (!isset($_POST['id']) || empty($_POST['id'])) {
            echo $this->error();
            return false;
        }
        // $data = array();
        $role = RoleModel::getInstance()->getRoleById($_POST['id']);
        if ($role['nodes'] && strpos($role['nodes'], ',') !== false) {
            $allowNodes = explode(',', $role['nodes']);
        } else {
            $allowNodes = [$role['nodes']];
        }
        $groups = NodeModel::getInstance()->getNodeGroups();
        $allNodes = NodeModel::getInstance()->getNodes();
        // foreach ($groups as $key => $group) {
        //     $data[$group['groups']] = array();
        // }
        foreach ($allNodes as $key => $node) {
            if (in_array($node['id'], $allowNodes)) {
                $node['allow'] = 1;
            }else{
                $node['allow'] = 0;
            }
            foreach ($groups as $k => $v) {
                if ($v['groups'] == $node['groups']) {
                    $groups[$k]['nodes'][] = $node;
                }
            }
        }
        $res = array(
            'node_group' => $groups,
            'role_name' => $role['role_name'],
            'id' => $role['id'],
            'nodes' => $role['nodes'],
        );
        echo $this->success($res);
        return true;
    }



}
